/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.dataprovider.control;

import com.compomics.dataprovider.view.wizard.MainGUI;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 *
 * @author Kenneth
 */
public class Main {

    //parameters
    private static File outputFile;
    private static List<String> filesToMerge;
    private static Options options;
    private static final CLIExecutor executor = new CLIExecutor();

    /**
     *
     * @param args
     * @throws IOException
     * @throws URISyntaxException
     */
    public static void main(String[] args) throws IOException, URISyntaxException {
        if (args.length == 0) {
            MainGUI.start();
            } else {
            constructOptions();
            parseCLI(args);
            executor.execute();
        }
    }

    private static void parseCLI(String[] args) {
        CommandLineParser parser = new GnuParser();
        try {
            CommandLine line = parser.parse(options, args);
            if (line.hasOption("help")) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("LyraPolicy", options);
            } else {
                //check for input options
                if (line.hasOption("download")) {
                    executor.setTaxonomiesToDownload(Arrays.asList(line.getOptionValue("download").split(",")));
                }
                if (line.hasOption("file")) {
                    executor.setFilesToMerge(Arrays.asList(line.getOptionValue("file").split(",")));
                }
                //check for output options
                if (!line.hasOption("out")) {
                    throw new IllegalArgumentException("The output should be declared !");
                } else {
                    executor.setOutputFile(new File(line.getOptionValue("out")));
                    if (filesToMerge != null && filesToMerge.contains(outputFile.getAbsolutePath())) {
                        throw new IllegalArgumentException("Input- and outputfiles can not be the same !");
                    }
                }
                //check for processing options
                executor.setIsReviewOnly(line.hasOption("reviewed"));
                executor.setIsDecoy(line.hasOption("decoy"));
                executor.setDeleteDuplicateSequences(line.hasOption("rem_dup_seq"));
                executor.setDeleteDuplicateAccessions(line.hasOption("rem_dup_acc"));
                executor.setIsCrap(line.hasOption("crap"));
                executor.setIsAppend(line.hasOption("append"));
            }
        } catch (ParseException exp) {
            // oops, something went wrong
            System.err.println("Parsing failed. Reason: " + exp.getMessage());
        }
    }

    private static void constructOptions() {
        //options
        options = new Options();
        options.addOption(new Option("help", "prints help message"));
        //input options
        options.addOption(new Option("file", "File input, as a comma separated list (ex file1.fasta,file2.fasta,file3.fasta)"));
        options.addOption(new Option("download", "The taxonomies to be downloaded to the fasta file as a comma separated list (ex 9606,10090,9823)"));
        options.addOption(new Option("review", "Consider the reviewed sequences in UniProt ?"));
        //processing options
        options.addOption(new Option("decoy", "adds decoy sequences at the end of the pipeline"));
        options.addOption(new Option("crap", "adds common contaminant sequences at the end of the pipeline"));
        options.addOption(new Option("rem_dup_seq", "removes duplicate sequences in the fasta file"));
        options.addOption(new Option("rem_dup_acc", "removes duplicate accessions in the fasta file"));
        //output options
        options.addOption(new Option("out", "The fasta output file"));
        options.addOption(new Option("append", "Append to an existing file?"));
    }

}
